﻿using Abp.AspNetCore;
using Abp.AspNetCore.Configuration;
using Abp.MailKit;
using Abp.Modules;
using Abp.Quartz;
using Abp.Runtime.Caching.Redis;
using Abp.SmsKit;
using Abp.Web.Models;
using HCD.Configuration;
using HCD.Const;
using HCD.EntityFrameworkCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using System;

namespace HCD.Web.Startup
{
    /// <summary>
    ///
    /// </summary>
    [DependsOn(
        typeof(AbpQuartzModule),
        typeof(AbpNetSmsModule), 
        typeof(AbpMailKitModule),
        typeof(EnterpriseApplicationModule),
        typeof(EnterpriseEntityFrameworkCoreModule),
        typeof(AbpAspNetCoreModule),
        typeof(AbpRedisCacheModule))]
    public class EnterpriseWebModule : AbpModule
    {
        private readonly IConfigurationRoot _appConfiguration;

        /// <summary>
        ///
        /// </summary>
        /// <param name="env"></param>
        public EnterpriseWebModule(IWebHostEnvironment env)
        {
            _appConfiguration = AppConfigurations.Get(env.ContentRootPath, env.EnvironmentName);
        }

        /// <summary>
        ///
        /// </summary>
        public override void PreInitialize()
        {
            Configuration.DefaultNameOrConnectionString = _appConfiguration.GetConnectionString(EnterpriseConsts.ConnectionStringName);

            //Configuration.Navigation.Providers.Add<EnterpriseNavigationProvider>();
            Configuration.Modules.AbpAspNetCore().SetNoCacheForAjaxResponses = false;
            var result = new DontWrapResultAttribute();
            Configuration.Modules.AbpAspNetCore().DefaultWrapResultAttribute.WrapOnError = result.WrapOnError;
            Configuration.Modules.AbpAspNetCore().DefaultWrapResultAttribute.WrapOnSuccess = result.WrapOnSuccess;

            Configuration.Modules.AbpAspNetCore()
                .CreateControllersForAppServices(
                    typeof(EnterpriseApplicationModule).Assembly,
                    moduleName: "v1",
                    useConventionalHttpVerbs: true
                );
            Configuration.Caching.UseRedis(options =>
            {
                options.ConnectionString = _appConfiguration["RedisCache:ConnectionStrings"];
                options.DatabaseId = _appConfiguration.GetValue<int>("RedisCache:DatabaseId");
            });

            //配置Default Cache的默认相对过期时间为30min
            Configuration.Caching.Configure(RedisConst.Default,cache =>
            {
                cache.DefaultSlidingExpireTime = TimeSpan.FromMinutes(30);
            });
            //登录错误次数记录默认绝对过期时间为1hr
            Configuration.Caching.Configure(RedisConst.AccessFailed, cache =>
            {
                cache.DefaultAbsoluteExpireTime = TimeSpan.FromHours(1);
            });
            //使用 Hangfire替换ABP默认的后台工作管理器。
            //Configuration.BackgroundJobs.UseHangfire();
            //关闭租户模式
            //Configuration.MultiTenancy.IsEnabled = true;
            Configuration.Settings.Providers.Add<HostEmailSettingProvider>();
            Configuration.Settings.Providers.Add<HostSmsSettingProvider>();
        }

        /// <summary>
        /// 注入容器
        /// </summary>
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(typeof(EnterpriseWebModule).Assembly);
        }
        /// <summary>
        /// 后台工人
        /// </summary>
        public override void PostInitialize()
        {
            //var workManager = IocManager.Resolve<IBackgroundWorkerManager>();
            //workManager.Add(IocManager.Resolve<WeatherWorker>());
        }

    }
}